package tree.searchTree;

import pojo.TreeNode;

/**
 * 功能描述: 二叉搜索树 (这个代码可用,但是呢,有更好的,SearchTreeUtils02)
 *          左子树 < 根节点 < 右子树 每一颗子树都是这样子的才叫搜索二叉树
 * 2024/05/18
 * <p>
 * ZhangYi
 */
public class SearchTreeUtils {

    public static  boolean isSearchTree(TreeNode root){
        if (root == null){
            return true;
        }

        return isSearchTreeHelper(root,null,null);
    }

    private static boolean isSearchTreeHelper(TreeNode root,TreeNode left,
                                        TreeNode right) {
        if (root == null ){
            return true;
        }
        //代码中使用 getId() 而非 val 或其他属性来获取节点的值。确保 TreeNode 类中有 getId() 方法，并返回节点的值。
        boolean isBST = (left == null || left.getId() - root.getId() < 0)
                && (right == null || right.getId() - root.getId() > 0)
                && isSearchTreeHelper(left.left,left,left.right)
                && isSearchTreeHelper(right.left,right,right.right);
        return isBST;
    }


    public static void main(String[] args) {
        TreeNode root = new TreeNode(21, "1");
        root.left = new TreeNode(1, "2");
        root.right = new TreeNode(3, "3");
        System.out.println(isSearchTree(root));
    }

}
